package com.google.gwt.it2tool.client.widget;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class StatusPanel extends VerticalPanel {
	
	private static final int MESSAGE_DISPLAY_TIME = 5000; //5 secs
	
	public StatusPanel(){
		super();
		
		addStyleName("statusPanel");
	}
	
	public void addErrorMessage(String error){		
		addMessage(false, error);
	}
	
	public void addSuccessMessage(String success){
		addMessage(true, success);
	}
	
	private void addMessage(boolean success, String msg){
		Label label;
		if (success){
			label = getSuccessLabel(msg);
		}else{
			label = getErrorLabel(msg);
		}	
		
		add(label);		
		setExpirationTimer(label);
	}
	
	private void setExpirationTimer(final Label label){
		// setup timer to delete the message after a specific amount of time
		Timer deleteTimer = new Timer() {
			public void run()
			{
				remove(label);
			}
		};
		deleteTimer.schedule(MESSAGE_DISPLAY_TIME);
	}
	
	private Label getErrorLabel(String error){
		Label ret = new Label("Fehler: " + error);
		ret.addStyleName("errorMessage");
		return ret;
	}
	
	private Label getSuccessLabel(String success){
		Label ret = new Label("Success: " + success);
		ret.addStyleName("positiveChange");
		return ret;
	}
	

}
